/*
 Copyright (c) 2014 ABB Group
 All rights reserved. This program and the accompanying materials
 are made available under the terms of the Eclipse Public License v1.0
 which accompanies this distribution, and is available at
 http://www.eclipse.org/legal/epl-v10.html
 
 Contributors:
    K. Eric Harper (ABB Corporate Research)
*/

using System;
using EAFacade;
using EAFacade.Model;

namespace DecisionArchitect.Model
{
    public interface IForceConnector
    {
        string Name { get; }
        string ConnectorGUID { get; }
        string DiagramGUID { get; set; }
    }

    public class ForceConnector : IForceConnector
    {
        private const char Separator = ':';

        public ForceConnector(string connectorGUID)
        {
            try
            {
                IEAConnector connector = EAMain.Repository.GetConnectorByGUID(connectorGUID);
                Name = connector.Name;
                ConnectorGUID = connectorGUID;
            }
            catch (ArgumentNullException e)
            {
                throw new ConnectorNotInModelException("No Connector with the specified GUID can be found. It has been probably deleted.", e);
            }
        }

        public string Name { get; private set; }
        public string ConnectorGUID { get; private set; }
        public string DiagramGUID { get; set; }

        public void DeserializeName(string value)
        {
            DiagramGUID = value.Split(Separator)[1];
        }

        public string SerializeName()
        {
            return String.Format(EATaggedValueKeys.IsForceConnector + Separator + "{0}", DiagramGUID);
        }

        public bool IsForceElementTag(string name)
        {
            return name.StartsWith(EATaggedValueKeys.IsForceConnector);
        }
    }

    public class ConnectorNotInModelException : Exception
    {
        public ConnectorNotInModelException(string message, Exception innerException) : base(message,innerException)
        {
           
        }
    }
}